﻿using Engine;
using System;
using System.Collections.Generic;

namespace CommandBlock
{
    /// <summary>
    /// 圆球区域
    /// </summary>
    public class SphereArea
    {
        public int Radius;
        public int SquaRadius;
        public Point3 Center;
        public Point3 Current;
        public Point3 MaxPoint;
        public Point3 MinPoint;

        public SphereArea(int r, Point3 c)
        {
            Radius = r;
            SquaRadius = r * r;
            Center = c;
            Current = Center;
            MaxPoint = Center + new Point3(Radius);
            MinPoint = Center - new Point3(Radius);
        }

        public void Ergodic(Action action)
        {
            for (int x = -Radius + 1; x < Radius; x++)
            {
                for (int y = -Radius + 1; y < Radius; y++)
                {
                    for (int z = -Radius + 1; z < Radius; z++)
                    {
                        if (x * x + y * y + z * z <= SquaRadius)
                        {
                            Current = new Point3(Center.X + x, Center.Y + y, Center.Z + z);
                            action.Invoke();
                        }
                    }
                }
            }
        }

        public bool Exist(Point3 target)
        {
            Point3 p = target - Center;
            if (p.X * p.X == SquaRadius || p.Y * p.Y == SquaRadius || p.Z * p.Z == SquaRadius)
            {
                return false;
            }
            return (p.X * p.X + p.Y * p.Y + p.Z * p.Z <= SquaRadius);
        }
    }
}